package vip.xiaonuo.supplier.modular.base.brands.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.base.brands.entity.SupplierBrands;
import vip.xiaonuo.supplier.modular.base.brands.mapper.SupplierBrandsMapper;
import vip.xiaonuo.supplier.modular.base.brands.param.SupplierBrandsAddParam;
import vip.xiaonuo.supplier.modular.base.brands.param.SupplierBrandsEditParam;
import vip.xiaonuo.supplier.modular.base.brands.param.SupplierBrandsIdParam;
import vip.xiaonuo.supplier.modular.base.brands.param.SupplierBrandsPageParam;
import vip.xiaonuo.supplier.modular.base.brands.service.SupplierBrandsService;

import java.util.List;

/**
 * 品牌信息Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/02/28 21:42
 **/
@Service
public class SupplierBrandsServiceImpl extends ServiceImpl<SupplierBrandsMapper, SupplierBrands> implements SupplierBrandsService {

    @Override
    public Page<SupplierBrands> page(SupplierBrandsPageParam supplierBrandsPageParam) {
        QueryWrapper<SupplierBrands> queryWrapper = new QueryWrapper<SupplierBrands>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierBrandsPageParam.getBrandName())) {
            queryWrapper.lambda().like(SupplierBrands::getBrandName, supplierBrandsPageParam.getBrandName());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierBrands::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierBrandsPageParam.getSortField(), supplierBrandsPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierBrandsPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierBrandsPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierBrandsPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierBrands::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierBrandsAddParam supplierBrandsAddParam) {
        SupplierBrands supplierBrands = BeanUtil.toBean(supplierBrandsAddParam, SupplierBrands.class);
        //数据归属
        supplierBrands.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierBrands);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierBrandsEditParam supplierBrandsEditParam) {
        SupplierBrands supplierBrands = this.queryEntity(supplierBrandsEditParam.getId());
        BeanUtil.copyProperties(supplierBrandsEditParam, supplierBrands);
        this.updateById(supplierBrands);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierBrandsIdParam> supplierBrandsIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierBrandsIdParamList, SupplierBrandsIdParam::getId));
    }

    @Override
    public SupplierBrands detail(SupplierBrandsIdParam supplierBrandsIdParam) {
        return this.queryEntity(supplierBrandsIdParam.getId());
    }

    @Override
    public SupplierBrands queryEntity(String id) {
        SupplierBrands supplierBrands = this.getById(id);
        if(ObjectUtil.isEmpty(supplierBrands)) {
            throw new CommonException("品牌信息不存在，id值为：{}", id);
        }
        return supplierBrands;
    }
}
